<template>
  <div>
    <el-row>
      <el-col style="margin-top: 10px">
        <el-table
          class="box-table"
          :data="Data"
          :stripe="true"
          :border="true"
          :fit="true"
        >
          <el-table-column
            prop="deviceNo"
            label="设备号"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            prop="name"
            label="设备名称"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            prop="electricQuantity"
            label="电量"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            prop="signal"
            label="信号"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            prop="userName"
            label="用户"
            width="auto"
            align="center"
            header-align="center"
          >
          <template #default="scope">
              <p
                v-if="scope.row.userName == null"
                style="color: red"
              >
                未绑定用户
              </p>
              <p
                v-else
                style="color: green"
              >
                {{ scope.row.userName }}
              </p>
            </template>
          </el-table-column>
          <el-table-column
            prop="deviceStatus.name"
            label="状态"
            width="auto"
            align="center"
            header-align="center"
          >
            <template #default="scope">
              <p
                v-if="scope.row.deviceStatus.name == '离线'"
                style="color: red"
              >
                离线
              </p>
              <p
                v-if="scope.row.deviceStatus.name == '在线'"
                style="color: green"
              >
                在线
              </p>
            </template>
          </el-table-column>
          <el-table-column
            prop="updateTime"
            label="最新上传时间"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="auto"
          >
            <template #default="scope">
              <el-button
                link
                type="primary"
                size="small"
                @click="
                  info(scope.row);
                  dialogVisible = true;
                "
                >设置手环上传频率</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col style="margin: 0 auto">
        <el-card>
          <div class="block">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="pageSize"
              :total="Total"
            >
            </el-pagination>
          </div>
        </el-card>
      </el-col>

      <el-dialog
        v-model="dialogVisible"
        title="设置手环上传频率(按秒) "
        width="30%"
        :before-close="handleClose"
      >
        <el-select
          v-model="pinlv_type"
          placeholder="选择设置频率类型"
          size="large"
        >
          <el-option
            v-for="item in weilan_options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <div style="width: 80%; margin: 20px 20px">
          <el-slider
            v-if="pinlv_type != 0"
            v-model="pinlv"
            :step="10"
            show-stops
            show-input
            :min="300"
            :max="2000"
          />
          <el-slider
            v-else
            v-model="pinlv"
            :step="10"
            show-stops
            show-input
            :min="20"
            :max="99"
          />
        </div>

        <el-button @click="ret_pinlu" style="margin-left: 25%; width: 50%"
          >提交</el-button
        >
      </el-dialog>
    </el-row>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, toRaw, onMounted, onBeforeMount } from "vue";
import apiService from "@/services/apiService";
import { conversionUtcDate, base64ToExcelAndDownload } from "@/utils/auth";
import { v4 as uuidv4 } from "uuid";
import { ElMessageBox } from "element-plus";
import { GetDevices, UpdateFrequency } from "@/api/api";
import { getToken } from "@/utils/auth";
import { message } from "@/utils/message";
defineOptions({
  // name 作为一种规范最好必须写上并且和路由的name保持一致
  name: "Shouhuan"
});

//表格数据
const Data = ref<any>([
  // {
  //   userid: 'SN1',
  //   userage: '98',
  //   usersex:'100'
  // }
]);
const currentPage = ref(1); // 当前页数
const pageSize = ref(10); // 每页条数
const Total = ref<number>(1); //总条数

//获取表单数据
const GetList = () => {
  Data.value = [];
  const data = ref({
    token: getToken().accessToken,
    body: {
      pageNumber: currentPage.value,
      pageSize: pageSize.value
    }
  });
  GetDevices(data.value)
    .then((res: any) => {
      console.log("表单数据", res);
      Data.value.push(...res.data.devices);
      Total.value = res.data.totalItems;
    })
    .catch((err: any) => {
      console.log(err);
    });
};

//详情数据
const msg_info = ref<any>([]);
const info = row => {
  msg_info.value = row.recorddetails;
  now_deviceNo.value = row.deviceNo;
};

//设置频率
const pinlv_type = ref<any>("");
const pinlv = ref<any>("");
const now_deviceNo = ref<any>("");
const weilan_options = [
  {
    value: 1,
    label: "心率"
  },
  {
    value: 2,
    label: "体温"
  },
  {
    value: 0,
    label: "位置"
  }
];
const ret_pinlu = () => {
  console.log("频率类型", pinlv_type.value);
  console.log("频率", pinlv.value);

  const data = ref({
    token: getToken().accessToken,
    body: {
      deviceNo: String(now_deviceNo.value),
      time: String(pinlv.value),
      type: pinlv_type.value
    }
  });
  UpdateFrequency(data.value)
    .then((res: any) => {
      console.log(res);
      if (res.code == 200) {
        console.log("111");

        message("设置频率成功", { type: "success" });
      }
    })
    .catch(err => {
      console.log(err);
    });
};

// 监听页码变化
const handleCurrentChange = val => {
  currentPage.value = val;
  GetList();
};

// 监听每页条数变化
const handleSizeChange = val => {
  pageSize.value = val;
};

const dialogVisible = ref(false);

const handleClose = (done: () => void) => {
  // ElMessageBox.confirm("确认关闭?")
  //   .then(() => {
  done();
  // })
  // .catch(() => {
  //   // catch error
  // });
};
onBeforeMount(() => {
  GetList();
  console.log(68500 + 21300 + 4500 * 6 + 11000 + 5000 + 2000 + 1340 * 4);
});
onMounted(() => {
  // GetList();
});
</script>

<style lang="scss" scoped></style>
